package cn.itcast.wushangxiantu.Fragment.home;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.UserData.UserValue;
import cn.itcast.wushangxiantu.UserData.UserYouXiang;
import cn.itcast.wushangxiantu.Activity.Bag;
import cn.itcast.wushangxiantu.Activity.Boss;
import cn.itcast.wushangxiantu.Activity.Chongwu;
import cn.itcast.wushangxiantu.Activity.ChouJiang;
import cn.itcast.wushangxiantu.Activity.DuoRenFuBen;
import cn.itcast.wushangxiantu.Activity.FuBen;
import cn.itcast.wushangxiantu.Activity.GuoZhanActivity;
import cn.itcast.wushangxiantu.Activity.GuoZhanShenQinActivity;
import cn.itcast.wushangxiantu.Activity.HeCheng;
import cn.itcast.wushangxiantu.Activity.JiaoYiSuo;
import cn.itcast.wushangxiantu.Activity.LingShi;
import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.Activity.PaiHang;
import cn.itcast.wushangxiantu.Activity.YouXiang;
import cn.itcast.wushangxiantu.Activity.ZhanShan;
import cn.itcast.wushangxiantu.Activity.ZhuangBeiTuPo;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class MainCity extends Fragment implements View.OnClickListener {
    private View view;
    private Context context;
    private TextView tvShengming;
    private TextView tvFangyu;
    private TextView tvGongji;
    private TextView tvBaoji;
    private TextView tvHuifu;
    private TextView tvChuantou;
    private TextView tvMingzhong;
    private TextView tvShanbi;
    private TextView tvWuqi;
    private TextView tvHuju;
    private TextView tvJiezhi;
    private TextView tvZuju;
    private TextView tvBag;
    private TextView tvHeCheng;
    private TextView tvJiaoyisuo;
    private TextView tvLingshi;
    private TextView tvYouxiang;
    private int number;
    private TextView tvZhanshan;
    private TextView tvCDK;
    private TextView tvChouJiang;
    private LinearLayout llQiTa;
    private TextView tvQiTa;
    private TextView tvPaiHang;
    private TextView tvBoss;
    private TextView tvFuBen;
    private TextView tvChongWu;
    private String[] chongwuname;
    private TextView tvChongWuLan;
    private TextView tvZhuangbeiChongzhu;
    private TextView tvContent;
    private TextView tvDuoRenFuBen;
    private TextView tvGuoZhan;
    private Button btnContent;

    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main_city, container, false);
        initView(view);
        EventBus.getDefault().register(this);
        getYouXiang();
        getLiXian();
        return view;
    }

    private void getLiXian() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&need=getlixian", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("getlixian1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("getlixian2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 4;
                handler.sendMessage(message);
            }
        });

    }

    public MainCity(Context context) {
        this.context = context;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void initView(View view) {
        Resources res = getResources();
        chongwuname = res.getStringArray(R.array.chongwu);
        tvShengming = (TextView) view.findViewById(R.id.tvShengming);
        tvFangyu = (TextView) view.findViewById(R.id.tvFangyu);
        tvGongji = (TextView) view.findViewById(R.id.tvGongji);
        tvBaoji = (TextView) view.findViewById(R.id.tvBaoji);
        tvHuifu = (TextView) view.findViewById(R.id.tvHuifu);
        tvChuantou = (TextView) view.findViewById(R.id.tvChuantou);
        tvMingzhong = (TextView) view.findViewById(R.id.tvMingzhong);
        tvShanbi = (TextView) view.findViewById(R.id.tvShanbi);
        tvWuqi = (TextView) view.findViewById(R.id.tvWuqi);
        tvHuju = (TextView) view.findViewById(R.id.tvHuju);
        tvJiezhi = (TextView) view.findViewById(R.id.tvJiezhi);
        tvZuju = (TextView) view.findViewById(R.id.tvZuju);
        tvContent = (TextView) view.findViewById(R.id.tvContent);

        UserValue userValue = new UserValue(Login.getUser());
        tvShengming.setText("生命值：" + userValue.getSm());
        tvBaoji.setText("暴击率：" + userValue.getBj() + "%");
        tvFangyu.setText("防御力：" + userValue.getFy());
        tvGongji.setText("攻击力：" + userValue.getGj());
        tvShanbi.setText("闪避值：" + userValue.getSb() + "%");
        tvMingzhong.setText("命中率：" + userValue.getMz() + "%");
        tvHuifu.setText("恢复：" + userValue.getHf());
        tvChuantou.setText("穿透值：" + userValue.getCt());
        tvContent.setText(Login.getUser().getContent());


        tvWuqi.setText(Login.getUser().getZhuangbei().get(0).getName());
        tvHuju.setText(Login.getUser().getZhuangbei().get(1).getName());
        tvJiezhi.setText(Login.getUser().getZhuangbei().get(2).getName());
        tvZuju.setText(Login.getUser().getZhuangbei().get(3).getName());

        setTextColor();
        tvBag = (TextView) view.findViewById(R.id.tvBag);
        tvBag.setOnClickListener(this);
        tvWuqi.setOnClickListener(this);
        tvZuju.setOnClickListener(this);
        tvJiezhi.setOnClickListener(this);
        tvHuju.setOnClickListener(this);

        tvHeCheng = (TextView) view.findViewById(R.id.tvHeCheng);
        tvHeCheng.setOnClickListener(this);
        tvJiaoyisuo = (TextView) view.findViewById(R.id.tvJiaoyisuo);
        tvJiaoyisuo.setOnClickListener(this);
        tvLingshi = (TextView) view.findViewById(R.id.tvLingshi);
        tvLingshi.setOnClickListener(this);
        tvYouxiang = (TextView) view.findViewById(R.id.tvYouxiang);
        tvYouxiang.setOnClickListener(this);
        tvZhanshan = (TextView) view.findViewById(R.id.tvZhanshan);
        tvZhanshan.setOnClickListener(this);
        tvCDK = (TextView) view.findViewById(R.id.tvCDK);
        tvCDK.setOnClickListener(this);
        tvChouJiang = (TextView) view.findViewById(R.id.tvChouJiang);
        tvChouJiang.setOnClickListener(this);
        llQiTa = (LinearLayout) view.findViewById(R.id.llQiTa);
        tvQiTa = (TextView) view.findViewById(R.id.tvQiTa);
        tvQiTa.setOnClickListener(this);
        tvPaiHang = (TextView) view.findViewById(R.id.tvPaiHang);
        tvPaiHang.setOnClickListener(this);
        tvBoss = (TextView) view.findViewById(R.id.tvBoss);
        tvBoss.setOnClickListener(this);
        tvFuBen = (TextView) view.findViewById(R.id.tvFuBen);
        tvFuBen.setOnClickListener(this);
        tvChongWu = (TextView) view.findViewById(R.id.tvChongWu);
        tvChongWu.setOnClickListener(this);
        tvChongWuLan = (TextView) view.findViewById(R.id.tvChongWuLan);
        tvChongWuLan.setOnClickListener(this);
        tvChongWuLan.setText("【" + chongwuname[Login.getUser().getCwid()] + "】");
        tvZhuangbeiChongzhu = (TextView) view.findViewById(R.id.tvZhuangbeiChongzhu);
        tvZhuangbeiChongzhu.setOnClickListener(this);
        tvDuoRenFuBen = (TextView) view.findViewById(R.id.tvDuoRenFuBen);
        tvDuoRenFuBen.setOnClickListener(this);
        tvGuoZhan = (TextView) view.findViewById(R.id.tvGuoZhan);
        tvGuoZhan.setOnClickListener(this);
        btnContent = (Button) view.findViewById(R.id.btnContent);
        btnContent.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void setTextColor() {
        if (Login.getUser().getWq2() >= 290) {
            tvWuqi.setTextColor(ContextCompat.getColor(context, R.color.wuqi4));
        } else if (Login.getUser().getWq2() > 260) {
            tvWuqi.setTextColor(ContextCompat.getColor(context, R.color.wuqi3));
        } else if (Login.getUser().getWq2() > 200) {
            tvWuqi.setTextColor(ContextCompat.getColor(context, R.color.wuqi2));
        } else if (Login.getUser().getWq2() > 0) {
            tvWuqi.setTextColor(ContextCompat.getColor(context, R.color.wuqi1));
        }

        if (Login.getUser().getHj2() >= 290) {
            tvHuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi4));
        } else if (Login.getUser().getHj2() > 260) {
            tvHuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi3));
        } else if (Login.getUser().getHj2() > 200) {
            tvHuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi2));
        } else if (Login.getUser().getHj2() > 0) {
            tvHuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi1));
        }

        if (Login.getUser().getJz2() >= 290) {
            tvJiezhi.setTextColor(ContextCompat.getColor(context, R.color.wuqi4));
        } else if (Login.getUser().getJz2() > 260) {
            tvJiezhi.setTextColor(ContextCompat.getColor(context, R.color.wuqi3));
        } else if (Login.getUser().getJz2() > 200) {
            tvJiezhi.setTextColor(ContextCompat.getColor(context, R.color.wuqi2));
        } else if (Login.getUser().getJz2() > 0) {
            tvJiezhi.setTextColor(ContextCompat.getColor(context, R.color.wuqi1));
        }

        if (Login.getUser().getZj2() >= 290) {
            tvZuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi4));
        } else if (Login.getUser().getZj2() > 260) {
            tvZuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi3));
        } else if (Login.getUser().getZj2() > 200) {
            tvZuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi2));
        } else if (Login.getUser().getZj2() > 0) {
            tvZuju.setTextColor(ContextCompat.getColor(context, R.color.wuqi1));
        }
    }

    @SuppressLint("WrongConstant")
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBag:
                Intent intent = null;
                intent = new Intent(context, Bag.class);
                startActivity(intent);
                break;
            case R.id.tvWuqi:
                if (!(Login.getUser().getZhuangbei().get(0).getName().equals("【空】"))) {
                    GeneralDialogDaoju(Login.getUser().getZhuangbei().get(0).getName(), getWuqiShuxing(0), "wq", 1);
                }
                break;
            case R.id.tvHuju:
                if (!(Login.getUser().getZhuangbei().get(1).getName().equals("【空】"))) {
                    GeneralDialogDaoju(Login.getUser().getZhuangbei().get(1).getName(), getWuqiShuxing(1), "hj", 2);
                }
                break;
            case R.id.tvJiezhi:
                if (!(Login.getUser().getZhuangbei().get(2).getName().equals("【空】"))) {
                    GeneralDialogDaoju(Login.getUser().getZhuangbei().get(2).getName(), getWuqiShuxing(2), "jz", 3);
                }
                break;
            case R.id.tvZuju:
                if (!(Login.getUser().getZhuangbei().get(3).getName().equals("【空】"))) {
                    GeneralDialogDaoju(Login.getUser().getZhuangbei().get(3).getName(), getWuqiShuxing(3), "zj", 4);
                }
                break;
            case R.id.tvHeCheng:
                intent = new Intent(context, HeCheng.class);
                startActivity(intent);
                break;
            case R.id.tvJiaoyisuo:
                if (Login.getUser().getDj() < 10) {
                    ToastUtil.toast(context, "抱歉我们这里只欢迎10级以上的上仙");
                } else {
                    intent = new Intent(context, JiaoYiSuo.class);
                    startActivity(intent);
                }
                break;
            case R.id.tvLingshi:
                intent = new Intent(context, LingShi.class);
                startActivity(intent);
                break;
            case R.id.tvYouxiang:
                intent = new Intent(context, YouXiang.class);
                startActivity(intent);
                break;
            case R.id.tvZhuangbeiChongzhu:
                intent = new Intent(context, ZhuangBeiTuPo.class);
                startActivity(intent);
                break;
            case R.id.tvZhanshan:
                if (Login.getUser().getDj() >= 10) {
                    intent = new Intent(context, ZhanShan.class);
                    startActivity(intent);
                } else {
                    ToastUtil.toast(context, "10级都没有你玩什么玩");
                }
                break;
            case R.id.tvCDK:
                GeneralDialog();
                break;
            case R.id.tvChouJiang:
                if (Login.getUser().getDj() >= 10) {
                    intent = new Intent(context, ChouJiang.class);
                    startActivity(intent);
                } else {
                    ToastUtil.toast(context, "10级都没有你玩什么玩");
                }
                break;
            case R.id.tvQiTa:
                if (llQiTa.getVisibility() == 0) {
                    llQiTa.setVisibility(View.GONE);
                    tvQiTa.setText("展开");
                } else {
                    llQiTa.setVisibility(View.VISIBLE);
                    tvQiTa.setText("隐藏");
                }
                break;
            case R.id.tvGuoZhan:
                if (Login.getUser().getDj() >= 20) {
                    getGuoJia(Login.getUser().getUsernumber(), Login.getUser().getPassword());
                } else {
                    ToastUtil.toast(context, "前方战场过于激烈请上仙20级再来参战");
                }
                break;
            case R.id.tvPaiHang:
                intent = new Intent(context, PaiHang.class);
                startActivity(intent);
                break;
            case R.id.tvBoss:
                intent = new Intent(context, Boss.class);
                startActivity(intent);
                break;
            case R.id.tvFuBen:
                intent = new Intent(context, FuBen.class);
                startActivity(intent);
                break;
            case R.id.tvChongWu:
                intent = new Intent(context, Chongwu.class);
                startActivity(intent);
                break;
            case R.id.tvChongWuLan:
                if (Login.getUser().getCwid() != 0) {
                    GeneralDialogDaoju(tvChongWuLan.getText().toString(), "具体介绍去技能图鉴看", "cw", 5);
                }
                break;
            case R.id.tvDuoRenFuBen:
                intent = new Intent(context, DuoRenFuBen.class);
                startActivity(intent);
                break;
            case R.id.btnContent:
                if (btnContent.getText().toString().trim().equals("显示")) {
                    tvContent.setVisibility(View.VISIBLE);
                    btnContent.setText("隐藏");
                } else {
                    btnContent.setText("显示");
                    tvContent.setVisibility(View.GONE);
                }
                break;
        }
    }

    /**
     * 获取玩家国家信息
     *
     * @param usernumber
     * @param password
     */
    private void getGuoJia(String usernumber, String password) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumber + "&passWord=" + password + "&need=getguojia", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                try {
                    Log.v("getGuoJia", e.getMessage());
                } catch (Exception e1) {

                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Log.v("getGuoJia2", res);
                Message message = new Message();
                User user = new Gson().fromJson(res, User.class);
                message.what = 5;
                message.obj = user;
                handler.sendMessage(message);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onStart() {
        initView(view);
        getYouXiang();
        super.onStart();
    }


    public String getWuqiShuxing(int i) {
        String content = "";
        content = "武器名称；" + Login.getUser().getZhuangbei().get(i).getName() + "\n" +
                "使用等级：" + Login.getUser().getZhuangbei().get(i).getSydj() + "\n******装备属性******\n";
        switch (i) {
            case 0:
                content += "攻击力：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() * 40 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getWq2() / 50))) + "\n";
                if (((int) (Login.getUser().getZhuangbei().get(i).getJsdj() / 5 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getWq2() / 1000))) > 45) {
                    content += "暴击率：45\n";
                } else {
                    content += "暴击率：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() / 5 +
                            Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getWq2() / 1000))) + "\n";
                }
                if (Login.getUser().getWq2() > 290) {
                    content += "品质：传说\n\n";
                } else if (Login.getUser().getWq2() > 260) {
                    content += "品质：史诗\n\n";
                } else if (Login.getUser().getWq2() > 200) {
                    content += "品质：精良\n\n";
                } else {
                    content += "品质：普通\n\n";
                }
                break;
            case 1:
                content += "生命：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() * 100 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getHj2() / 50))) + "\n";
                content += "防御力：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() * 20 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getHj2() / 100))) + "\n";
                if (Login.getUser().getHj2() > 290) {
                    content += "品质：传说\n\n";
                } else if (Login.getUser().getHj2() > 260) {
                    content += "品质：史诗\n\n";
                } else if (Login.getUser().getHj2() > 200) {
                    content += "品质：精良\n\n";
                } else {
                    content += "品质：普通\n\n";
                }
                break;
            case 2:
                if (((int) (Login.getUser().getZhuangbei().get(i).getJsdj() / 5 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getJz2() / 1000))) > 45) {
                    content += "命中：45\n";
                } else {
                    content += "命中：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() / 5 +
                            Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getJz2() / 1000))) + "\n";
                }

                content += "穿透：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() * 10 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getJz2() / 50))) + "\n";
                if (Login.getUser().getJz2() > 290) {
                    content += "品质：传说\n\n";
                } else if (Login.getUser().getJz2() > 260) {
                    content += "品质：史诗\n\n";
                } else if (Login.getUser().getJz2() > 200) {
                    content += "品质：精良\n\n";
                } else {
                    content += "品质：普通\n\n";
                }
                break;
            case 3:
                if (((int) (Login.getUser().getZhuangbei().get(i).getJsdj() / 5 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getZj2() / 1000))) > 45) {
                    content += "闪避：45\n";
                } else {
                    content += "闪避：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() / 5 +
                            Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getZj2() / 1000))) + "\n";
                }
                content += "恢复：" + ((int) (Login.getUser().getZhuangbei().get(i).getJsdj() * 10 +
                        Login.getUser().getZhuangbei().get(i).getJsdj() * ((double) Login.getUser().getZj2() / 50))) + "\n";
                if (Login.getUser().getZj2() > 290) {
                    content += "品质：传说\n\n";
                } else if (Login.getUser().getZj2() > 260) {
                    content += "品质：史诗\n\n";
                } else if (Login.getUser().getZj2() > 200) {
                    content += "品质：精良\n\n";
                } else {
                    content += "品质：普通\n\n";
                }
        }

        return content;
    }

    public void GeneralDialog(String head, String content) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         * 3位置
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)

                .setMessage(content)

                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }


    public void GeneralDialogDaoju(String head, String content, final String weizhi, final int num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         * 3位置
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)

                .setMessage(content)

                .setNegativeButton("卸下", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        number = num;
                        getDate(weizhi, num);
                        dialogInterface.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void getDate(String weizhi, final int num) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&banben=" + Login.getVerName(context) + "&passWord=" + Login.getUser().getPassword() + "&need=xiexia" + "&Name=" + Login.getUser().getName() + "&weizhi=" + weizhi, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("xiexiazhuangbei1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("xiexiazhuangbei2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @SuppressLint("NewApi")
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    User user = (User) message.obj;
                    if (user != null) {
                        if (user.getStw().equals("Yes")) {
                            ToastUtil.toast(context, "操作成功");
                            switch (number) {
                                case 1:
                                    tvWuqi.setText("【空】");
                                    break;
                                case 2:
                                    tvHuju.setText("【空】");
                                    break;
                                case 3:
                                    tvJiezhi.setText("【空】");
                                    break;
                                case 4:
                                    tvZuju.setText("【空】");
                                    break;
                                case 5:
                                    tvChongWuLan.setText("【空】");
                                    Login.getUser().setCw_id(0);
                                    break;
                            }
                            Login.getUser().setHj2(user.getHj2());
                            Login.getUser().setWq2(user.getWq2());
                            Login.getUser().setJz2(user.getJz2());
                            Login.getUser().setZj2(user.getZj2());
                            Login.getUser().setZhuangbei(user.getZhuangbei());
                        } else {
                            ToastUtil.toast(context, user.getStw());
                        }
                    } else {
                        ToastUtil.toast(context, "服务器坏掉了");
                    }
                    break;
                case 2:
                    UserYouXiang userYouXiang = (UserYouXiang) message.obj;
                    if (userYouXiang != null) {
                        if (userYouXiang.getYouXiang().size() != 0) {
                            tvYouxiang.setTextColor(Color.RED);
                        } else {
                            tvYouxiang.setTextColor(Color.BLACK);
                        }
                    } else {
                        ToastUtil.toast(context, "服务器坏掉了");
                    }
                    break;
                case 3:
                    user = (User) message.obj;
                    if (null != user) {
                        ToastUtil.toast(context, user.getStw());
                    } else {
                        ToastUtil.toast(context, "服务器坏掉了");
                    }
                    break;
                case 4:
                    user = (User) message.obj;
                    if (null != user) {
                        if (user.getStw().equals("OK")) {
                            GeneralDialog("离线详情", "离线时长:" + user.getMoney() + "分钟\n" + user.getContent());
                        } else {
                            ToastUtil.toast(context, user.getStw());
                        }
                    } else {
                        ToastUtil.toast(context, "服务器坏掉了");
                    }
                    break;
                case 5:
                    user = (User) message.obj;
                    if (null != user) {
                        if (user.getStw().equals("0")) {
                            //表示无国家
                            Intent intent = new Intent(context, GuoZhanShenQinActivity.class);
                            startActivity(intent);
                        } else {
                            //表示有国家
                            Intent intent = new Intent(context, GuoZhanActivity.class);
                            intent.putExtra("gj_id", Integer.parseInt(user.getStw().trim()));
                            startActivity(intent);
                        }
                    } else {
                        ToastUtil.toast(context, "服务器坏掉了");
                    }
                    break;
            }
            return false;
        }
    });

    private void GeneralDialog() {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setLines(1);
        editText.setInputType(InputType.TYPE_CLASS_TEXT);
        String content = null;
        content = "输入兑换码";
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(content)
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (Login.getUser().getDj() < 5) {
                            ToastUtil.toast(context, "5级开放");
                            return;
                        }
                        fasongCDK(Login.getUser().getUsernumber(), editText.getText().toString().trim());
                        dialogInterface.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void fasongCDK(String usernumber, String trim) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumber + "&need=CDK" + "&content=" + trim, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("cdk1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("cdk2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 3;
                handler.sendMessage(message);
            }
        });
    }

    /**
     * 接收后处理
     *
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shuaxing(MessageEvent messageEvent) {
        if (messageEvent.getMessage() == "YouXiang") {
            getYouXiang();
        }
    }

    private void getYouXiang() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=youxiangyulan", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("youxiangyulan1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("youxiangyulan2", src);
                UserYouXiang user = null;
                try {
                    user = new Gson().fromJson(src, UserYouXiang.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }
}
